﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Filters;
using System.Web.Routing;
using System.Web.Security;

namespace First.Filter
{
    public class MyAuthorizaAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            //string path = filterContext.HttpContext.Request.Path;
            //StreamReader stream = new StreamReader(System.Web.HttpContext.Current.Request.InputStream);

            //var actionName2 = filterContext.ActionDescriptor.ActionName;
            //var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            var actionName = filterContext.RouteData.Values["action"].ToString();
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var area = filterContext.RouteData.Values["area"] ?? "";
           // string json = HttpUtility.UrlDecode(stream.ReadToEnd());
            if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
            {
                if (filterContext.HttpContext.Session["userInfo"] == null)
                {
                    filterContext.Result =
                        new RedirectToRouteResult(
                            new RouteValueDictionary(
                                new { Area = "Login", controller = "MyLogin", action = "Index" }));
                    return;
                }
                var user = filterContext.HttpContext.Session["userInfo"] as object;
                //HttpCookie httpCookie=filterContext.HttpContext.Request.Cookies.Get("userRole");
                //if (httpCookie == null)
                //{
                //  httpCookie= new HttpCookie("userRole", "user");
                //  filterContext.HttpContext.Response.SetCookie(httpCookie);
                //}
            }
        }
    }
}